package api

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"go-caipu/pkg/api/helper"
	"go-caipu/pkg/constant"
	"go-caipu/pkg/services/admin/menu"
	"go-caipu/pkg/services/admin/sysuser"
	"go-caipu/pkg/util"
	"time"
)

// GetAsyncRoutes 异步获取路由菜单（主菜单）
func (hs *HTTPServer) GetAsyncRoutes(c *gin.Context) {
	cmd := sysuser.GetUserCommand{UserId: hs.getJwtUserID(c)}
	user, err := hs.userService.GetUser(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	result, err3 := hs.menuService.GetMenuWithRole(c.Request.Context(), user.RoleIds)
	if err3 != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}

// GetMenuList 查询菜单列表
func (hs *HTTPServer) GetMenuList(c *gin.Context) {
	result, err := hs.menuService.GetMenuList(c.Request.Context())
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}

// GetMenuTreeSelect 菜单选择树
func (hs *HTTPServer) GetMenuTreeSelect(c *gin.Context) {
	result, err := hs.menuService.MenuTreeSelect(c.Request.Context())
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}

// GetMenu 菜单查询
func (hs *HTTPServer) GetMenu(c *gin.Context) {
	cmd := menu.GetMenuCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, err := hs.menuService.GetMenu(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}
func (hs *HTTPServer) CreateMenu(c *gin.Context) {
	cmd := menu.Menu{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	cmd.MenuId = hs.Snowflake.Generate().Int64()
	err := hs.menuService.Insert(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}
func (hs *HTTPServer) UpdateMenu(c *gin.Context) {
	cmd := menu.Menu{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}

	err := hs.menuService.Update(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) RemoveMenu(c *gin.Context) {
	cmd := menu.RemoveMenuCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	//cmd.Id = util.StringToInt64(c.Param("id"))
	//if cmd.Id > 0 {
	//	cmd.Ids = []int64{cmd.Id}
	//}
	err := hs.menuService.Remove(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}
func (hs *HTTPServer) UpdateMenuApi(c *gin.Context) {
	menu := menu.UpdateMenuApiCommand{}
	if err := helper.BindAndValidate(c, &menu); err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	menu.MenuId = util.StringToInt64(c.Param("id"))
	menu.UserId = hs.getJwtUserID(c)
	err := hs.menuService.UpdateMenuApi(c.Request.Context(), menu)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
	}
	helper.SuccessWithOutData(c)
}

// ExportMenu 导出菜单(插件菜单初使化使用）
func (hs *HTTPServer) ExportMenu(c *gin.Context) {
	//menuId := util.StringToInt64(c.Query("id"))
	//if menuId == 0 {
	//	helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, errors.New("menuId is empty"))
	//	return
	//}
	cmd := menu.GetMenuCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	result, _ := hs.menuService.ExportMenu(c.Request.Context(), cmd)
	if len(result) == 0 {
		c.Writer.Write([]byte("未找到符合的menu记录"))
	}
	c.Header("Content-Disposition", "attachment; filename="+fmt.Sprintf("sys-menu-%s.json", time.Now().Format("20060102150405")))
	jsonData, _ := json.Marshal(result)
	c.Writer.Write(jsonData)
}
